<style scoped lang="scss">
@import url('index.scss');
</style>

<template>
  <div class="app-container">
    <!--新模版样式-->
    <div class="topsearch">
      <el-row>
        <el-col :span="22" class="searchcol">
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="86px">
            <el-form-item label="班次名称" prop="shitName">
              <el-input v-model="queryParams.shitName" placeholder="请输入班次名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="所在院区" prop="hid">
              <el-input v-model="queryParams.hid" placeholder="请输入所在院区" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="所在科室" prop="kid">
              <el-input v-model="queryParams.kid" placeholder="请输入所在科室" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否晚班" prop="isNight">
              <el-input v-model="queryParams.isNight" placeholder="请输入是否晚班" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否假日班" prop="isHoliday">
              <el-input v-model="queryParams.isHoliday" placeholder="请输入是否假日班" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button class="newButton" type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" class="rightcol">
          <div class="newbtn" @click="handleAdd" v-hasPermi="['system:schtype:add']">
            <img src="@/assets/images/add.svg" alt="">
            <span>新建</span>
          </div>
          <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:schtype:edit']">
            <img src="@/assets/images/edit.svg" alt=""></img>
            <span>编辑</span>
          </div>
          <div class="newbtn" v-if="multiple == false" @click="handleDelete" v-hasPermi="['system:schtype:remove']">
            <img src="@/assets/images/del.svg" alt=""></img>
            <span>删除</span>
          </div>
          <div class="newbtn" @click="handleExport" v-hasPermi="['system:schtype:export']">
            <img src="@/assets/images/export.svg" alt=""></img>
            <span>导出</span>
          </div>
          <div class="newbtn" @click="getList">
            <img src="@/assets/images/refresh.svg" alt=""></img>
            <span>刷新</span>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="newtable">
      <el-table v-loading="loading" :data="schtypeList" @selection-change="handleSelectionChange"
        :cell-style="{ backgroundColor: '#f9f9f9' }">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" width="55"  align="center" prop="sid" />
        <el-table-column label="班次编码" align="center" prop="shiftCode" />
        <el-table-column label="班次名称" align="center" prop="shitName" />
        <el-table-column label="所在院区" align="center" prop="hid" />
        <el-table-column label="所在科室" align="center" prop="kid">
          <template #default="scope">
            {{departmentList.find(item => item.kid == scope.row.kid)?.kname || '-'}}
          </template>
        </el-table-column>
        <el-table-column label="班次开始时间" align="center" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ scope.row.startTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="班次结束时间" align="center" prop="endTime" width="180">
          <template #default="scope">
            <span>{{ scope.row.endTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否晚班" align="center" prop="isNight">
          <template #default="scope">
            <el-tag :type="scope.row.isNight == 1 ? 'success' : 'danger'">{{ scope.row.isNight == 1 ? '是' : '否'
              }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否假日班" align="center" prop="isHoliday">
          <template #default="scope">
            <el-tag :type="scope.row.isHoliday == 1 ? 'success' : 'danger'">{{ scope.row.isHoliday == 1 ? '是' : '否'
              }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="可接诊数量" align="center" prop="defaultnum" />
        <el-table-column label="班次状态" align="center" prop="status" >
          <template #default="scope">
            {{ ha_normal_status.find(item => item.value == scope.row.status)?.label || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:schtype:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:schtype:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>



    <!-- 添加或修改基础班次对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" class="newDialog">
      <el-form ref="schtypeRef" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="班次名称" prop="shitName">
              <el-input v-model="form.shitName" placeholder="请输入班次名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班次编码" prop="shiftCode">
              <el-input v-model="form.shiftCode" placeholder="请输入班次编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所在院区" prop="hid">
              <el-input v-model="form.hid" placeholder="请输入所在院区" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在科室" prop="kid">
              <!-- <el-input v-model="form.kid" placeholder="请输入所在科室" /> -->
              <el-select v-model="form.kid" placeholder="请选择所在科室">
                <el-option v-for="item in departmentList" :key="item.kid" :label="item.kname" :value="item.kid" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="班次开始时间" prop="startTime">
              <!-- <el-date-picker clearable v-model="form.startTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择班次开始时间">
          </el-date-picker> -->
              <el-time-select v-model="form.startTime" style="width: 100%" :max-time="form.endTime"
                placeholder="请选择班次开始时间" start="07:30" step="00:30" end="18:30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="班次结束时间" prop="endTime">
              <el-date-picker clearable v-model="form.endTime" type="date" value-format="YYYY-MM-DD"
                placeholder="请选择班次结束时间">
              </el-date-picker> -->
            <el-form-item label="班次结束时间" prop="endTime">
              <el-time-select v-model="form.endTime" style="width: 100%" :min-time="form.startTime"
                placeholder="请选择班次结束时间" start="07:30" step="00:30" end="23:30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否晚班" prop="isNight">
              <el-radio-group v-model="form.isNight">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否假日班" prop="isHoliday">
              <!-- <el-input v-model="form.isHoliday" placeholder="请输入是否假日班" /> -->
              <el-radio-group v-model="form.isHoliday">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="可接诊数量" prop="defaultnum">
              <el-input v-model="form.defaultnum" placeholder="请输入可接诊数量" type="number" min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in ha_normal_status" :key="dict.value" :value="dict.value" :label="dict.value">{{
              dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :rows="6" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Schtype">
import { listSchtype, getSchtype, delSchtype, addSchtype, updateSchtype } from "@/api/system/schtype"
import { listDepartment } from "@/api/system/department"

const { proxy } = getCurrentInstance()
const { ha_normal_status } = proxy.useDict('ha_normal_status'); // 使用字典

const schtypeList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shiftCode: null,
    shitName: null,
    hid: null,
    kid: null,
    startTime: null,
    endTime: null,
    isNight: null,
    isHoliday: null,
    defaultnum: null,
    status: null,
  },
  rules: {
    shiftCode: [{ required: true, message: '请输入班次编码', trigger: 'blur' }],
    shitName: [{ required: true, message: '请输入班次名称', trigger: 'blur' }],
    kid: [{ required: true, message: '请选择科室', trigger: 'change' }],
    startTime: [{ required: true, message: '请选择班次开始时间', trigger: 'change' }],
    endTime: [{ required: true, message: '请选择班次结束时间', trigger: 'change' }],
    defaultnum: [{ required: true, message: '请输入可接诊数量', trigger: 'blur' }],
  }
})

const { queryParams, form, rules } = toRefs(data)
const newqueryParams = {
  pageNum: 1,
  pageSize: 1000,
}
// 获取科室列表
const departmentList = ref([])

onMounted(async () => {
  //列出所有的科室
  getDepartmentList()
//end

})
// 获取科室列表@鲍小伟20251111
function getDepartmentList() {
  listDepartment(newqueryParams).then(response => {
    console.log(response)
    if(response.code == 200){
      departmentList.value = response.rows
    } else {
      departmentList.value = []
    }
  })
}


/** 查询基础班次列表 */
function getList() {
  loading.value = true
  listSchtype(queryParams.value).then(response => {
    schtypeList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    sid: null,
    shiftCode: null,
    shitName: null,
    hid: null,
    kid: null,
    startTime: null,
    endTime: null,
    isNight: '0',
    isHoliday: '0',
    defaultnum: null,
    status: '1',
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("schtypeRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.sid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加基础班次"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _sid = row.sid || ids.value
  getSchtype(_sid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改基础班次"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["schtypeRef"].validate(valid => {
    if (valid) {
      if (form.value.sid != null) {
        updateSchtype(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addSchtype(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _sids = row.sid || ids.value
  proxy.$modal.confirm('是否确认删除基础班次编号为"' + _sids + '"的数据项？').then(function() {
    return delSchtype(_sids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/schtype/export', {
    ...queryParams.value
  }, `schtype_${new Date().getTime()}.xlsx`)
}

getList()
</script>
